package com.xbongbong.paas.toolbox.util;

import com.xbongbong.saas.constant.BasicConstant;


import java.util.Random;

/**
 * 为营销促销生成券码，指令码
 *
 * @author jyh
 * @version v1.0
 * @since 2020/8/10 7:04 下午
 */
public class MarketingTokenUtil {

    public static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String SPECIAL_MARK = "#";

    public static void main(String[] args) throws Exception {

        String encryptToken = getEncryptToken("1", 9000);
        System.out.println(encryptToken);
        System.out.println(encryptToken.length());
    }


    public static String getEncryptToken(String corpid, Integer businessType) {
        String str = corpid + businessType + DateTimeUtil.getInt();
        String tokenStr = DigestUtil.Encrypt(str, "MD5");
        String randomStr = generateString(BasicConstant.SIX);
        StringBuffer sb = new StringBuffer();
        sb.append(SPECIAL_MARK).append(tokenStr).append(randomStr).append(SPECIAL_MARK);
        return sb.toString();
    }

    public static boolean easyValidate(String hashCode) {
        if (StringUtil.isEmpty(hashCode) || hashCode.length() != BasicConstant.FORTY
                || !hashCode.startsWith(SPECIAL_MARK) || !hashCode.endsWith(SPECIAL_MARK)) {
            return false;
        }
        return true;
    }





    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length())));
        }
        return sb.toString();
    }

    
}
